//
//  HMNewViewController.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/8.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit

class HMNewViewController: UIViewController ,UIScrollViewDelegate{

     lazy var newPageScroll : UIScrollView = {
        let tempScroll = UIScrollView()
        tempScroll.delegate = self
        tempScroll.pagingEnabled = true
        tempScroll.showsHorizontalScrollIndicator = false
        tempScroll.backgroundColor = UIColor.orangeColor()
        return tempScroll
    }()
    
    lazy var PageControl : UIPageControl = {
        
        let page = UIPageControl()
        
        page.currentPageIndicatorTintColor = UIColor.blackColor()
    
        page.pageIndicatorTintColor = UIColor.whiteColor()
        return page
    }()
    lazy var enterButton : UIButton = {
    
        let button = UIButton()
        
        button.setTitle("进入微博", forState: UIControlState.Normal)
        
        button.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.backgroundColor = UIColor.orangeColor()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        return button
    }()
    
    lazy var selectshare : UIButton = {
    
        let button = UIButton()
        button.addTarget(self, action: "shareChlik:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        button.setTitle(" 分享微博", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.sizeToFit()
        return button
    }()
    @objc private func enterButtonClick(){
        
        
    }
    @objc private func shareChlik(button : UIButton){
    
        button.selected = !button.selected
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let page = newPageScroll.contentOffset.x / newPageScroll.bounds.width
        
        PageControl.currentPage = Int(page)
    }
    
    
    func setupUI(){
    
        newPageScroll.frame = view.bounds
        view .addSubview(newPageScroll)
        
        PageControl.frame = CGRect(x: newPageScroll.bounds.width*0.5, y: newPageScroll.bounds.height-100, width: 0, height: 0)
        view .addSubview(PageControl)
        let count = 4
        for i in 0..<count
        {
            
            let newImage = UIImage(named: "new_feature_\(i+1)")
            
            let newimageview = UIImageView(image: newImage)
            
            newimageview.frame = CGRect(x: CGFloat(i)*(newPageScroll.bounds.width), y: 0, width: newPageScroll.bounds.width, height: newPageScroll.bounds.height)
            
            newPageScroll .addSubview(newimageview)
            if(i==(count-1))
            {
            
                self.imageAddButton(newimageview)
            }
            
        }
        newPageScroll.contentSize = CGSize(width: CGFloat(count)*newPageScroll.bounds.width, height: 0)
        PageControl.numberOfPages = count
    }

    func imageAddButton(imagView : UIImageView){
        
        imagView.userInteractionEnabled = true
        enterButton.center = CGPoint(x: imagView.bounds.width * 0.5, y: imagView.bounds.height-200)
        selectshare.center = CGPoint(x: imagView.bounds.width * 0.5, y: CGRectGetMinY(enterButton.frame)-50)
        
        imagView .addSubview(enterButton)
        imagView .addSubview(selectshare)
    }
}
